// pages/product/index/index.js
import {getList} from "../../../service/Product";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        actionSheet: { //类型选择
            show: false,
            list: [{
                value: "c_fund_name",
                name: "按产品名称"
            }, {
                value: "-n_nav",
                name: "按单位净值（从高到低）"
            }, {
                value: "-n_added_nav",
                name: "按累计净值（从高到低）"
            }, {
                value: "-n_lastest_m_return",
                name: "按月涨幅（从高到低）"
            }],
        },
        product: {
            list: [],
            loading: false,
            reset: false,
            error: false,
            total: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this._requestListData = {}
        this.setData({"product.loading": true})
    },

    /*
    * 搜索
    * */
    onSearch: function (e) {
        let value = e.detail;
        let currentValue = this._requestListData.c_fund_name;
        if (value !== currentValue) {
            this._requestListData.c_fund_name = value
            this.setData({
                "product.list": [],
                "product.reset": true
            })
        }
    },
    onSearchClear: function () {
        let params = this._requestListData;
        if (params.c_fund_name) {
            delete params.c_fund_name;
            this._requestListData = params
            this.setData({
                "product.list": [],
                "product.reset": true
            })
        }
    },

    /*
    * 监听列表加载
    * */
    onLoadList: async function (e) {
        try {
            let data = this._requestListData;
            let paging = e.detail.paging;
            let list = this.data.product.list;
            let isReset = this.data.product.reset;
            let res = await getList({
                "page": paging.page,
                "per-page": paging.pageSize,
                ...data
            });
            let newList = res.itemList;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "product.list": list,
                "product.reset": false,
                "product.loading": false,
                "product.total": res.totalCount
            });
        } catch (e) {
            this.setData({
                "product.error": true
            })
        }
    },

    onSelect: function () {
        this.setData({"actionSheet.show": true})
    },
    onActionSheetClose: function () {
        this.setData({"actionSheet.show": false})
    },
    onActionSheetSelect: function (e) {
        let {value} = e.detail;
        let currentValue = this._requestListData.sort;
        if (value !== currentValue) {
            this._requestListData.sort = value;
            this.setData({
                "product.list": [],
                "product.reset": true
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"product.loading": true})
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})